import { login,  getInfo, refreshtoken } from '@/api/login'//logout,
import { gettoken, settoken, setExpiresIn, removetoken } from '@/utils/auth'
// import { encrypt, decrypt } from '@/utils/jsencrypt'
import {encrypt, decrypt } from '@/utils/aes'

const user = {
  state: {
    token: gettoken(),
    name: localStorage.getItem('name'),
    avatar: localStorage.getItem('avatar'),
    phone: localStorage.getItem('phone'),
    // roles: [],
    // permissions: [],
    // roleName: "",
    // deptName: "",
    isLoginDialogVisible: false ,
    currentmenu:'1',
    orderType:0,
    customerId:localStorage.getItem('customerId'),//我的菜单选中状态,
    searchText:'',//搜索框内容,
    searchTextbydeptId:'',//店铺搜索框内容,
    eleObj:localStorage.getItem('eleObj')?JSON.parse(localStorage.getItem('eleObj')):{},//电梯详情
  },

  mutations: {
    SET_eleObj: (state, eleObj) => {
      state.eleObj = {...eleObj}
      console.log(eleObj)
      localStorage.setItem('eleObj', JSON.stringify(eleObj))
    },
    showLoginDialog(state) {
      state.isLoginDialogVisible = true;
    },
    hideLoginDialog(state) {
      state.isLoginDialogVisible = false;
    },
    SET_searchText: (state, searchText) => {
      console.log(searchText)
      state.searchText =searchText
    },
    SET_searchTextbydeptId: (state, searchTextbydeptId) => {
       state.searchTextbydeptId =searchTextbydeptId
    },
    SET_customerId: (state, customerId) => {
      state.token = customerId
      settoken(customerId)
      
    },
    SET_currentmenu: (state, currentmenu) => {
      state.currentmenu = currentmenu
    },
    SET_orderType: (state, orderType) => {
      state.orderType = orderType
    },
    SET_token: (state, token) => {
      state.token = token
    },
    SET_EXPIRES_IN: (state, time) => {
      state.expires_in = time
    },
    SET_phone: (state, phone) => {
      state.phone = phone
      localStorage.setItem('phone', phone)
       console.log(localStorage.getItem('phone'))
    },
    SET_NAME: (state, name) => {
      state.name = name
      localStorage.setItem('name', name)
       console.log(localStorage.getItem('name'))
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
      localStorage.setItem('avatar', avatar)
       console.log(localStorage.getItem('avatar'))

    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_PERMISSIONS: (state, permissions) => {
      state.permissions = permissions
    },
    SET_ROLENAME: (state, roleName) => {
      state.roleName = roleName
    },
    SET_DEPTNAME: (state, deptName) => {
      state.deptName = deptName
    },
  },

  actions: {
    setname({ commit },name){
      console.log(name)
      commit('SET_NAME',name)

    },

    // 改变我的菜单选中状态
    changeeleObj({ commit },data){
       console.log(data)
      commit('SET_eleObj',data)
     
    },
    openLoginDialog({ commit }) {
      commit('showLoginDialog');
    },
    closeLoginDialog({ commit }) {
      commit('hideLoginDialog');
    },
    // 改变我的菜单选中状态
    changeCustomerId({ commit },data){
       console.log(data)
      commit('SET_customerId',data)
    },
    // 改变我的菜单选中状态
    changeCurrentmenu({ commit },currentmenu){
      console.log(currentmenu)
      commit('SET_currentmenu',currentmenu)
    },
    // 改变我的菜单选中状态
    changeSearchText({ commit },data){
      // console.log(searchText)
      commit('SET_searchText',data)
    },
    changeSearchTextbydeptId({ commit },data){
       commit('SET_searchTextbydeptId',data)
    },
    // 改变订单当前选中状态
    changeOrderType({ commit },orderType){
      console.log(orderType)
      commit('SET_orderType',orderType)
    },
    // 登录
    Login({ commit }, userInfo) {
      const username = userInfo.username.trim()
      const password = userInfo.password
      const code = userInfo.code
      const uuid = userInfo.uuid
      return new Promise((resolve, reject) => {
        login(username, password, code, uuid).then(res => {
          console.log(res)
          settoken(res.token)
          commit('SET_customerId',res.token)
          commit('SET_NAME','梯语云用户')
          resolve(res.token)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 获取用户信息
    GetInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        getInfo().then(res => {
          var  post = res.post
          if(post&&post.length>0){
            var arr =[]
            post.map(item=>{
              arr.push(item.postName)
            })
            sessionStorage.setItem('post', JSON.stringify(arr))
          }
          const user = res.user
          sessionStorage.setItem('user', JSON.stringify(user))
          // require("")
          const avatar = (user.avatar == "" || user.avatar == null) ? "" : user.avatar;
          if (res.roles && res.roles.length > 0) { // 验证返回的roles是否是一个非空数组
            commit('SET_ROLES', res.roles)
            commit('SET_PERMISSIONS', res.permissions)
          } else {
            commit('SET_ROLES', ['ROLE_DEFAULT'])
          }
          commit('SET_NAME', user.nickName)
          commit('SET_AVATAR', avatar)
          commit('SET_ROLENAME', user.dept.deptName) //roles[0].roleName
          commit('SET_DEPTNAME', user.dept.deptName)
          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 刷新token
    Refreshtoken({ commit, state }) {
      return new Promise((resolve, reject) => {
        refreshtoken(state.token).then(res => {
          setExpiresIn(res.data)
          commit('SET_EXPIRES_IN', res.data)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 退出系统
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        commit('SET_customerId','')
        commit('SET_NAME','')
        commit('SET_AVATAR','')
        localStorage.removeItem('customerId')
        localStorage.removeItem('name')
        localStorage.removeItem('avatar')
        localStorage.removeItem('customer')
        resolve()
      })
    },

    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        commit('SET_token', '')
        removetoken()
        resolve()
      })
    }
  }
}

export default user
